package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/8/1 10:49
 * @description：https://leetcode-cn.com/problems/the-k-weakest-rows-in-a-matrix/
 */
public class 矩阵中战斗力最弱的K行 {
    @Test
    public void test() {
        Solution solution = new Solution();
        int[] ints = {1             ,0};
        System.out.println(solution.binary_search(ints, 1));
    }


    class Solution {
        public int[] kWeakestRows(int[][] mat, int k) {
            ArrayList<int[]> list = new ArrayList<>();
            for (int i = 0; i < mat.length; i++) {
                list.add(new int[] {binary_search(mat[i], 1) + 1, i});
            }
            list.sort((o1, o2) -> {
                if (o1[0] != o2[0]) return o1[0] - o2[0];
                return o1[1] - o2[1];
            });
            for (int[] ints : list) {
                System.out.println(Arrays.toString(ints));
            }
            int[] ans = new int[k];
            for (int i = 0; i < k; i++) {
                ans[i] = list.get(i)[1];
            }
            return ans;
        }

        private int binary_search(int[] arr, int tar) {
            int l = 0;
            int r = arr.length - 1;
            while (l < r) {
                int mid = l + r + 1 >> 1;
                if (arr[mid] >= tar) {
                    l = mid;
                } else {
                    r = mid - 1;
                }
            }
            return arr[l] == tar ? l : -1;
        }
    }
}
